package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwt.util.JwtUtils;
import com.jwt.util.RsaUtils;
import com.wfx.entity.Result;
import com.wfx.entity.WxbCustomer;
import com.wfx.entity.WxbGoodsSku;
import com.wfx.mapper.WxbGoodsSkuMapper;
import com.wfx.service.IWxbGoodsSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.security.PublicKey;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-25
 */
@Service
public class WxbGoodsSkuServiceImpl extends ServiceImpl<WxbGoodsSkuMapper, WxbGoodsSku> implements IWxbGoodsSkuService {
    @Autowired
    private WxbGoodsSkuMapper wxbGoodsSkuMapper;
    @Override
    public Result insertSku(List<WxbGoodsSku> wxbGoodsSkuList,Result result) {
        if (wxbGoodsSkuList.size() == 0) {
            return new Result(false,"参数异常");
        }
        try {
            String publicKeyPath = ResourceUtils.getFile("classpath:ras_public").getPath();
            PublicKey publicKey = RsaUtils.getPublicKey(publicKeyPath);

            //解析数据
            WxbCustomer wxbCustomer = (WxbCustomer) JwtUtils.getInfoFromToken(wxbGoodsSkuList.get(0).getSellerId(), publicKey, WxbCustomer.class);

            for (WxbGoodsSku wxbGoodsSku : wxbGoodsSkuList) {
                wxbGoodsSku.setGoodsId((Long) result.getData());
                wxbGoodsSku.setSellerId(wxbCustomer.getCustomerId());
                wxbGoodsSku.setTitle(wxbGoodsSku.getSpec());
                wxbGoodsSku.setCreateTime(new Date());
                wxbGoodsSku.setUpdateTime(new Date());
                Long l = Long.valueOf(560);
                wxbGoodsSku.setCategoryId(l);
                wxbGoodsSku.setStatus("1");
                wxbGoodsSkuMapper.insert(wxbGoodsSku);
            }
            return  new Result(true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result delOne(Long goodsId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_id",goodsId);
        int i = wxbGoodsSkuMapper.delete(queryWrapper);
        return new Result(i==1?true:false,i==1?"删除成功":"sku删除失败");
    }
    //根据商品id查询sku信息
    @Override
    public Result<WxbGoodsSku> findSku(String goodsId) {
        QueryWrapper<WxbGoodsSku> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        WxbGoodsSku wxbGoodsSku = wxbGoodsSkuMapper.selectOne(queryWrapper);
        if (wxbGoodsSku == null) {
            return new Result<>(false,"数据为空");
        }
        return new Result<>(true,"查询成功",wxbGoodsSku);
    }
}
